namespace EntityFrameworkStudy.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using System.Collections.Generic;

    using EntityFrameworkStudy.Studys;

    internal sealed class Configuration : DbMigrationsConfiguration<EntityFrameworkStudy.Studys.StudyContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(EntityFrameworkStudy.Studys.StudyContext context)
        {
            for (var i = 1; i <= 2; i++)
            {
                var article = new Article { Title = "Title" + i };
                context.Articles.AddOrUpdate(x => x.Title, article);
                context.SaveChanges();

                var post = new Post { Body = "Title" + i + "-Post", ArticleId = article.Id };
                context.Posts.AddOrUpdate(x => x.Body, post);
                context.SaveChanges();
            }

            for (var i = 1; i <= 2; i++)
            {
                var catalog = new Catalog { Title = "Title" + i };
                context.Catalogs.AddOrUpdate(x => x.Title, catalog);
                context.SaveChanges();

                var book = new Book { Body = "Title" + i + "-Post", CatalogId = catalog.Id };
                context.Books.AddOrUpdate(x => x.Body, book);
                context.SaveChanges();
            }

            for (var i = 1; i <= 2; i++)
            {
                var newsType = new NewsType { Title = "Title" + i };
                context.NewsTypes.AddOrUpdate(x => x.Title, newsType);
                context.SaveChanges();

                var news = new News { Body = "Title" + i + "-Post", NewsTypeId = newsType.Id };
                context.News.AddOrUpdate(x => x.Body, news);
                context.SaveChanges();
            }

            for (var i = 1; i <= 2; i++)
            {
                var user = new User { Title = "Title" + i };
                context.Users.AddOrUpdate(x => x.Title, user);
                context.SaveChanges();
            }

            for (var i = 1; i <= 2; i++)
            {
                var order = new Order { Id = i, Product = "Product" + i };
                context.Orders.AddOrUpdate(x => x.Product, order);
                context.SaveChanges();
            }

            for (var i = 1; i <= 2; i++)
            {
                var l1 = new Level1
                {
                    Level2s = new List<Level2>
                    { 
                        new Level2
                        {
                            Level3s = new List<Level3>
                            {
                                new Level3()
                            }
                        }
                    }
                };
                context.Level1s.Add(l1);
                context.SaveChanges();
            }
        }
    }
}
